﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Markup;
using System.Reflection;
using System.Windows.Media;
using SharpMap.Wpf.Rendering;

namespace SharpMap.Wpf.Markup
{
    //[MarkupExtensionReturnType(typeof(SharpMap.Styles.StyleColor))]
    public class StyleColorExtension : MarkupExtension
    {
        private string _suorce;

        public StyleColorExtension(string source)
        {
            _suorce = source;

        }

        //[ConstructorArgument(
        public string Source
        {
            get { return _suorce; }
            set { _suorce = value; }
        }


        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            IProvideValueTarget targetProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
            PropertyInfo propInfo;
            if (targetProvider != null &&
                 (propInfo = targetProvider.TargetProperty as PropertyInfo) != null)
            {
                if (propInfo.PropertyType. IsAssignableFrom(typeof(SharpMap.Styles.StyleColor)))
                {
                    Color color = (Color)ColorConverter.ConvertFromString(_suorce);
                    return ViewConverter.Convert(color);
                    //return SharpMap.Styles.StyleColor.Green;
                }
                if (propInfo.PropertyType.IsAssignableFrom(typeof(SharpMap.Styles.StyleBrush)))
                {
                    BrushConverter bc = new BrushConverter();
                    Brush b = (Brush)bc.ConvertFromString(_suorce);
                    return ViewConverter.Convert(b);
                }
            }
            return SharpMap.Styles.StyleColor.Transparent;
        }
    }
}
